<?php
	error_reporting(E_ALL ^E_NOTICE);
	
	session_start();	
	if ((!isset($_SESSION['username'])) && (!isset($_SESSION['password']))) {
		header("location:index.php"); //not logged in, redirect to login page
	}
	$con = mysql_connect("localhost","root","") or die("Can't connect to database. Contact Your Administrator.");	
	mysql_select_db("gestation") or die("Cannot select DB. Contact your web administrator.");
	
	require_once('head.php');
?>
<script>
	$(document).ready(function() {
		$("#menu_1").css("color","#B6DD53");
		$("#menu_2").css("color","#035C8C");
		$("#menu_3").css("color","#035C8C");
		$('#motherName').autocomplete(
		{
			source: "search.php",
			minLength: 1
		});
		
		$('#biodata_ibu').hide();
		$('#input_pasien_baru').hide();
	});
	
	var dataIbu = 0;
	function showHideDataIbu() {
		if (dataIbu == 0) {
			$('#biodata_ibu').show(300);
			dataIbu = 1;
		}
		else {
			$('#biodata_ibu').hide(300);
			dataIbu = 0;
		}
	}
	
	var inputDataIbu = 0;
	function showHideInputDataIbu() {
		if (inputDataIbu == 0) {
			$('#input_pasien_baru').show(150);
			inputDataIbu = 1;
		}
		else {
			$('#input_pasien_baru').hide(150);
			inputDataIbu = 0;
		}
	}
</script>

<?php
	$tow = 0;
	
	if (isset($_POST['cariPasienButton'])) {
		/* get Patient's data */
		$motherName = $_POST['motherName'];
		$result = mysql_query("SELECT * FROM mother WHERE mother_name='$motherName'");
		while($row = mysql_fetch_array($result)){
			$motherID = $row['mother_id'];
			$_POST['motherID'] = $motherID;
			$motherName = $row['mother_name'];
			$motherAddr = $row['mother_address'];
			$motherHeight =  $row['mother_height'];
			$motherWeight =  $row['mother_weight'];
			$motherEtniss = $row['mother_etnis'];
			$motherParity = $row['mother_parity'];
			
			echo "
			<script>
				$(document).ready(function() {
					$('#mother_name').val('".$motherName."');
					$('#mother_id').val('".$motherID."');
					$('#pengukuran_mother_id').val('".$motherID."');
					$('#mother_address').html('".$motherAddr."');
					$('#mother_height').val('".$motherHeight."');
					$('#mother_weight').val('".$motherWeight."');
					
					$('#etnis_".$motherEtniss."').attr('selected', 'selected');
					
					if (".$motherParity." == 0) 
						$('#parity_0').attr('selected', 'selected');
					else
					if (".$motherParity." == 1) 
						$('#parity_1').attr('selected', 'selected');
					else
					if (".$motherParity." == 2) 
						$('#parity_2').attr('selected', 'selected');			
					else
					if (".$motherParity." >= 3) 
						$('#parity_3').attr('selected', 'selected');
				});
			</script>";
			
			switch ($motherEtniss) {
				case 0: $motherEtnis = -206.4; break;
				case 1: $motherEtnis = -156.8; break;
				case 2: $motherEtnis = -125.7; break;
				case 3: $motherEtnis = -166.0; break;
				case 4: $motherEtnis = -63.7; break;
				case 5: $motherEtnis = -90.0; break;
				case 6: $motherEtnis = 64.0; break;
				case 7: $motherEtnis = 71.5; break;
				case 8: $motherEtnis = -60; break;
			}
		}
		$result = mysql_query("SELECT * FROM embrio WHERE embrio_mother_id='$motherID'");
		while ( $row = mysql_fetch_array($result) ) {
			$embrioID = $row['embrio_id'];
			$embrioSex = $row['embrio_sex'];
			$embrioEDD = $row['embrio_edd'];
		}
		echo "<script> $(document).ready(function() {  
				$('#mother_edd').val('".$embrioEDD."'); 
				$('#embrio_id').val('".$embrioID."');
				if (".$embrioSex." == 0) 
					$('#sex_female').attr('selected', 'selected');
				else
				if (".$embrioSex." == 1) 
					$('#sex_male').attr('selected', 'selected');
				else
				if (".$embrioSex." == -1) 
					$('#sex_unknown').attr('selected', 'selected');
			}); 
		</script>";
	}
	
	if (isset($_POST['submitData'])) {
		$motherName = $_POST['input_mother_name'];
		$motherAddr = $_POST['input_mother_address'];			
		$motherWeight = $_POST['input_mother_weight'];
		$motherHeight = $_POST['input_mother_height'];
		$motherEtnis = $_POST['input_mother_etnis'];
		$motherPar = $_POST['input_mother_parity'];
		$embrioEDD = $_POST['input_mother_edd'];
		$embrioSex =  $_POST['input_kelamin'];
		
		$sql = "INSERT INTO mother(mother_name, mother_address, mother_etnis, mother_parity, mother_weight, mother_height) 
		VALUES('$motherName', '$motherAddr', '$motherEtnis', '$motherPar', '$motherWeight', '$motherHeight')";
		mysql_query($sql, $con) or die("Error Input Pasien Baru 1. Contact your administrator");
		$id = mysql_insert_id();
		
		$sql1 = "INSERT INTO embrio(embrio_mother_id, embrio_edd, embrio_sex) VALUES ('$id', '$embrioEDD', '$embrioSex')"; 
		mysql_query($sql1, $con) or die("Error Input Pasien Baru 2. Contact your administrator");
		
		echo "Data berhasil disimpan";
	}	
?>

<div id="container">
	<div id="saving_notification" style="color:red; display:none;"> Saving... </div>
	
	<!-- Form untuk Input Pasien Baru --> 
	<div id="input_pasien_baru">
		<form id="add_mother_form" name="add_mother_form" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
			<table border="0" id="add_mother_table">
				<tr>
					<td width="30%;"> Nama </td>
					<td width="70%;"> <input type="text" name="input_mother_name" id="input_mother_name" style="width:100%;" value=""/> </td>
				</tr>
				<tr>
					<td> Alamat </td>
					<td> <textarea name="input_mother_address" id="input_mother_address" rows="4" style="width:100%;"></textarea> </td>
				</tr>
				<tr>
					<td> EDD</td>
					<td> <input type="date" name="input_mother_edd" id="input_mother_edd" value=""/> </td>
				</tr>
				<tr>
					<td> Etnis </td>
					<td> 
						<select name="input_mother_etnis" id="input_mother_etnis">
							<option id="etnis_0" value="0"> Indian </option>	<!-- -206.4 -->
							<option id="etnis_1" value="1"> Pakistani </option> <!-- -156.8 -->
							<option id="etnis_2" value="2"> Bangladeshi </option> <!-- -125.7 -->
							<option id="etnis_3" value="3"> African Caribbean </option> <!-- -116 -->
							<option id="etnis_4" value="4"> African (sub Sahara) </option> <!-- -63.7 -->
							<option id="etnis_5" value="5"> Middle East (inc North Africa) </option> <!-- -90 -->
							<option id="etnis_6" value="6"> Far East Asian (eg China, Japan) </option> <!-- 64 -->
							<option id="etnis_7" value="7"> South East Asia (eg Thailand, Indonesia, Philippines) </option> <!-- 71.5 -->
							<option id="etnis_8" value="8"> Other </option> <!-- -60 -->
						</select>
					</td>
				
				</tr>
				<tr>
					<td> Tinggi </td>
					<td> <input type="text" name="input_mother_height" id="input_mother_height" value=""/> cm </td>
				</tr>
				<tr>
					<td> Berat </td>
					<td> <input type="text" name="input_mother_weight" id="input_mother_weight" value=""/> kg </td>
				</tr>
				<tr>
					<td> Parity </td>
					<td> 
						<select name="input_mother_parity" id="input_mother_parity">
							<option id="parity_0" value="0">	0  </option> <!-- value = 0 -->
							<option id="parity_1" value="1">	1  </option> <!-- value = +111.0 -->
							<option id="parity_2" value="2">	2  </option> <!-- value = +154.8 -->
							<option id="parity_3" value="3">	3+ </option> <!-- value = +151.3 -->
						</select>
					</td>
				</tr>
				<tr>
					<td> Jenis Kelamin Janin </td>
					<td align="left">
						<label for="select"></label>
						<select name="input_kelamin" id="input_kelamin">
							<option id="sex_unknown" value="-1"> Belum diketahui</option>
							<option id="sex_male" value="1"> Laki</option> <!-- value = +52.6 -->
							<option id="sex_female" value="0">Perempuan</option> <!-- value = -52.6 -->
						</select>
					</td>
				</tr>
				<tr>
					<td></td>
					<td> <br> <input type="submit" name="submitData" id="submitData" value="Submit" style="width:90px; height:30px;"/> </td>
				</tr>	
			</table>
		</form>		
	</div>
	
	<div id="form_container">
		<div class="form40"> 
			<form id="search_form" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
				<label class="left"> Nama Pasien </label> 
				<input type="text" name="motherName" id="motherName" class="left" value="<? if (isset($_POST['motherName'])) echo $_POST['motherName'];?>"/> 
				<input type="submit" name="cariPasienButton" class="left left10" value="Cari"> 
				<span id="edit_data_ibu_button" class="left" onclick="javascript:showHideDataIbu();"> Edit Data Pasien </span>
			</form>
			<div style="clear:both"> </div>
			
			<div id="biodata_ibu"> 
				<br> <div class="formTitle"> Data Ibu </div> 
				<form id="edit_mother_form" name="edit_mother_form" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
				<table id="data_individu" border="0" style="width:90%;">
					<tr>
						<td> <input type="hidden" name="mother_id" id="mother_id" value=""/> </td>
						<td> <input type="hidden" name="embrio_id" id="embrio_id" value=""/> </td>
					</tr>
					<tr>
						<td width="20%;"> Nama </td>
						<td width="80%;"> <input type="text" name="mother_name" id="mother_name" style="width:100%;" value=""/> </td>
					</tr>
					<tr>
						<td> Alamat </td>
						<td> <textarea name="mother_address" id="mother_address" rows="4" style="width:100%;"></textarea> </td>
					</tr>
					<tr>
						<td> EDD</td>
						<td> <input type="date" name="mother_edd" id="mother_edd" value=""/> </td> 
					</tr>
					<tr>
						<td> Etnis </td>
						<td>
							<select name="mother_etnis" id="mother_etnis">
								<option id="etnis_0" value="0"> Indian </option>	<!-- -206.4 -->
								<option id="etnis_1" value="1"> Pakistani </option> <!-- -156.8 -->
								<option id="etnis_2" value="2"> Bangladeshi </option> <!-- -125.7 -->
								<option id="etnis_3" value="3"> African Caribbean </option> <!-- -116 -->
								<option id="etnis_4" value="4"> African (sub Sahara) </option> <!-- -63.7 -->
								<option id="etnis_5" value="5"> Middle East (inc North Africa) </option> <!-- -90 -->
								<option id="etnis_6" value="6"> Far East Asian (eg China, Japan) </option> <!-- 64 -->
								<option id="etnis_7" value="7"> South East Asia (eg Thailand, Indonesia, Philippines) </option> <!-- 71.5 -->
								<option id="etnis_8" value="8"> Other </option> <!-- -60 -->
							</select>
						</td>
					
					</tr>
					<tr>
						<td> Tinggi </td>
						<td> <input type="text" name="mother_height" id="mother_height" value=""/> cm </td>
					</tr>
					<tr>
						<td> Berat </td>
						<td> <input type="text" name="mother_weight" id="mother_weight" value=""/> kg </td>
					</tr>
					<tr>
						<td> Parity </td>
						<td> 
							<select name="mother_parity" id="mother_parity">
								<option id="parity_0" value="0">	0  </option> <!-- value = 0 -->
								<option id="parity_1" value="1">	1  </option> <!-- value = +111.0 -->
								<option id="parity_2" value="2">	2  </option> <!-- value = +154.8 -->
								<option id="parity_3" value="3">	3+ </option> <!-- value = +151.3 -->
							</select>
						</td>
					</tr>
					<tr>
						<td> Kelamin Janin </td>
						<td align="left">
							<label for="select"></label>
							<select name="kelamin" id="kelamin">
								<option id="sex_unknown" value="-1"> Belum diketahui</option>
								<option id="sex_male" value="1"> Laki</option> <!-- value = 52.6 -->
								<option id="sex_female" value="0">Perempuan</option> <!-- value = -52.6 -->
							</select>
						</td>
					</tr>
					<tr>
						<td></td>
						<td> <a onclick="javascript:editDataPasien();" class="button_blue" style="width:60px;"> Update </a></td>
					</tr>				
				</table>
				</form> <br>
			</div>
			
			<br>
			<span class="formTitle"> Data Pemeriksaan Janin </span> 			
			<form id="pengukuranJaninForm" method="post">
				<div class="left">
					<label style="width:120px;" class="left"> Tanggal </label>
					<input type="date" name="pengukuran_tanggal" id="pengukuran_tanggal" value="<?php echo date('Y-m-d', strtotime(date('Y/m/d'))); ?>" class="left"> <br>
										
					<label style="width:120px;" class="left"> Tinggi Fundus </label>
					<input type="text" name="pengukuran_tinggi" id="pengukuran_tinggi" value="0" class="left"> <br>
					
					<input type="hidden" name="pengukuran_mother_id" id="pengukuran_mother_id">
					<a href="javascript:simpanHasilPengukuran();" class="button_blue left" style="margin-left:120px; margin-top:5px;"> OK </a>
				</div>
			</form>
			<br><br><br><br><br><br>
			
			<!-- Tabel Pengukuran Janin -->
			<table id="tabel_janin" border="1" cellpadding="3" cellspacing="0" style="margin-top:40px;">
				<tr>
					<th>&nbsp;</th>
					<th>Minggu</th>
					<th>Tinggi Fundus</th>
				</tr>
				<?php
				if (isset($_POST['motherID'])) {
					$measurementHeight = array();
					$measurementDate = array();
					$measurementRealDate = array();
					
					$result = mysql_query("SELECT * FROM measurement WHERE measurement_embrio_id='$embrioID'");
					$i = 1;
					while( $row = mysql_fetch_array($result) ){
						echo "<tr>";
						echo "<td>".$i."</td>";
						echo "<td id='date_".$row['measurement_date']."'>".$row['measurement_date']."</td>";
						echo "<td id='height_".$row['measurement_date']."'>".$row['measurement_height']."</td>";
						echo "</tr>";
						
						array_push($measurementHeight,$row['measurement_height']);
						array_push($measurementDate,$i);
						array_push($measurementRealDate,$row['measurement_date']);
						
						$i++;
					}
				}
				?>
				<tr id="tabelHasilPengukuran">
					
				</tr>
			</table>
			
			<!-- Data Populasi -->
			<div id="data_populasi" style="display:none;">
				<div class="formTitle"> Data Populasi </div> 
				<table id="data_populasi" style="width:90%;">
					<tr>
						<td width="55" scope="row"> Const </td>
						<td width="144">
							<label for="textfield"></label>
							<input name="const" type="text" id="textfield" value="3455.6" readonly="readonly"/>
						</td>
					</tr>
					<tr>
						<td scope="row"> Tinggi </td>
						<td> <input name="tinggi_populasi" type="text" id="textfield2" value="163" readonly="readonly" /></td>
						
					</tr>
					<tr>
						<td scope="row"> Berat </td>
						<td><input name="berat_populasi" type="text" id="textfield4" value="64" readonly="readonly" /></td>
					</tr>
					<tr>
						<td scope="row" >Parity</td>
						<td align="left"> <td>
					</tr>
					<tr>
						<td scope="row">Std</td>
						<td>
							<label for="std"></label>
							<input name="std" type="text" id="textfield6" value="389" readonly="readonly" />
						</td>
					</tr>
					<tr>
						<td style="font-weight:bold;"> TOW(Term Optimal Weight) : </td>
						<td> 
							<input name="tow" type="text" id="tow" readonly="readonly" <?php printf("value=$tow");?> />	
						</td>
					</tr>
				</table>
			</div>
		</div>
		<div style="display:block;float:left;width:2%;height:100%;">&nbsp;</div>
		
		<div class="form56">	
			<?php	
				function reloadGrafik($motherHeight, $motherWeight, $motherParity, $measurementDate, $measurementRealDate, $measurementHeight) {
					$kelamin = 0; 
					if ($embrioSex == 0) { //female
						$kelamin = -48.9; 
						/*
						echo "<script> 
						$('#sex_female').attr('selected', 'selected');
						</script>"; */
					}
					else 
					if ($embrioSex == 1) {	//male
						$kelamin = 48.9;
						/* echo "<script>
						$('#sex_male').attr('selected', 'selected');
						</script>"; */
					}
					
					$const = 3455.6;
					$tinggi_populasi = 163;
					$berat_populasi = 64;
					$etnis = 53.4;
					$std = 10;
					
					//untuk individu
					$tinggi_idv = $motherHeight;	
					$berat_idv = $motherWeight;
					$par = $motherParity;
									
					//$tanggal = $measurementDate;
					$tanggal = serialize($measurementDate);
					$realdate = serialize($measurementRealDate);
					$stinggi = serialize($measurementHeight);
					$i--;
					$tow = $const + ($tinggi_idv-$tinggi_populasi)*6.7 + ($berat_idv-$berat_populasi)*9.173 + $etnis + $par + $kelamin;
					printf ("<img src='make.php?const=$const&edd=$embrioEDD&std=$std&tow=$tow&realdate=$realdate&tanggal=$tanggal&tinggi=$stinggi&pemeriksaan=$i' width='747' height='600'/>");								
				}
			?>
			<div id="div_grafik">
				<?php 
				reloadGrafik($motherHeight, $motherWeight, $motherParity, $measurementDate, $measurementRealDate, $measurementHeight);
				?>
			</div>
		</div>
	</div>
	<div style="clear:both;"> <br><br> </div>
	
	<script>
		function simpanHasilPengukuran() {
			var tanggal = $('#pengukuran_tanggal').val();
			var tinggi = $('#pengukuran_tinggi').val();
			var motherID = $('#pengukuran_mother_id').val();
			
			if ( (tanggal == "") || (tinggi == "") || (motherID == "") ) {
				var pesan = " tidak boleh kosong.";
				if (tanggal == "") pesan = "tanggal," + pesan;
				if (tinggi == "") pesan = "tinggi," + pesan;
				if (motherID == "") pesan = "Pilih pasien terlebih dahulu.";
				alert("Error " + pesan);
				return;
			} 
			
			if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
				xmlhttp=new XMLHttpRequest();
			}
			else { // code for IE6, IE5
				xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
			}
			
			xmlhttp.onreadystatechange=function() {
				if (xmlhttp.readyState==4 && xmlhttp.status==200)
				{
					if ( xmlhttp.responseText == 'xxx' ) {
						document.getElementById("height_"+tanggal).innerHTML=tinggi;
					}
					else
					$('#tabel_janin').append(xmlhttp.responseText);	
					document.getElementById("div_grafik").innerHTML = $('#div_grafik').html();
				}
			}			
			xmlhttp.open("GET","make_table_janin.php?tinggi="+tinggi+"&tanggal="+tanggal+"&motherID="+motherID+"&pemeriksaan=$i",true);
			xmlhttp.send(); 
		}
		
		function editDataPasien() {
			var motherID = $('#mother_id').val();
			var embrioID = $('#embrio_id').val();
			var motherName = $('#mother_name').val();
			var motherAddr = $('#mother_address').val();			
			var motherWeight = $('#mother_weight').val();
			var motherHeight = $('#mother_height').val();
			var motherEtnis = $('#mother_etnis').val();
			var motherPar = $('#mother_parity').val();
			var embrioEDD = $('#mother_edd').val();
			var embrioSex =  $('#kelamin').val();

			if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
				xmlhttp=new XMLHttpRequest();
			}
			else { // code for IE6, IE5
				xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
			}
			
			xmlhttp.onreadystatechange=function() {
				if (xmlhttp.readyState==4 && xmlhttp.status==200)
				{
					if ( xmlhttp.responseText == 'oke' ) {
						showHideDataIbu();
						$('#saving_notification').fadeIn(300).delay(800).fadeOut(400);
					}
				}
			}			
			xmlhttp.open("GET","edit_data_pasien.php?mother_id="+motherID+"&embrio_id="+embrioID+"&mother_name="+motherName+
			"&mother_address="+motherAddr+"&mother_weight="+motherWeight+"&mother_height="+motherHeight+"&mother_etnis="+motherEtnis+
			"&mother_parity="+motherPar+"&mother_edd="+embrioEDD+"&kelamin="+embrioSex,true);
			xmlhttp.send(); 
		}
	</script>
	<?php
		require_once('footer.php');
		
	   mysql_close();	
	?>
